package com.ai.transcription.controller;

import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: springboot3-demos
 * @description:
 * @author: Reagan Li
 * @create: 2024-04-26 14:55
 **/

@RestController
@RequestMapping("/ai/transcription")
public class TranscriptionController {

    @Autowired
    private OpenAiAudioTranscriptionModel openAiAudioTranscriptionModel;

    @Autowired
    private OpenAiImageModel openAiImageModel;

    @RequestMapping(value = "/v1")
    public Object transcription() {
//        org.springframework.core.io.Resource audioFile = new ClassPathResource("jfk.flac");
        org.springframework.core.io.Resource audioFile = new ClassPathResource("cat.mp3");
        String called = openAiAudioTranscriptionModel.call(audioFile);
        System.out.println(called);

        return called;
    }

    @RequestMapping("/v2")
    private Object image2(@RequestParam(value = "msg") String msg) {
        ImageResponse imageResponse = openAiImageModel.call(new ImagePrompt(msg, OpenAiImageOptions.builder()
                .withQuality("hd") //高清图像
                .withN(1)  //生成1张图片
                .withHeight(1024) //生成的图片高度
                .withWidth(1024) //生成的图片宽度
                .build()));
        System.out.println(imageResponse);

        String imageUrl = imageResponse.getResult().getOutput().getUrl();
        //把图片进行业务处理
        return imageResponse.getResult().getOutput();
    }
}
